/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_sem.h"
#include "los_base.ph"
#include "los_hwi.h"
#include "los_api_sem.h"
#include "los_inspect_entry.h"
#ifdef LOSCFG_LIB_LIBC
#include "string.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

/* task prio */
#define TASK_PRIO_TEST  5

/* task pid */
static UINT32 g_TestTaskID01, g_TestTaskID02, g_TestTaskID03;
/* sem id */
static UINT32 g_usSemID;
extern UINT8 Task_finish_flag;
static VOID Example_SemTask1(VOID)
{
    UINT32 uwRet;

    dprintf("task1_lo try get sem g_usSemID forever .\r\n");
    /* get sem, timeout is forever */
    uwRet = LOS_SemPend(g_usSemID, LOS_WAIT_FOREVER);

    /* get sem ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task1_lo get sem ok.\r\n");
        dprintf("task1_lo post sem.\r\n");
        LOS_SemPost(g_usSemID);
        
    }

    /* task delay 40 ticks */
    LOS_TaskDelay(100);
    
    dprintf("task1_lo exit\r\n");
    Task_finish_flag++;
    LOS_TaskDelete(g_TestTaskID01);
    
    return;
}

static VOID Example_SemTask2(VOID)
{
    UINT32 uwRet;

    // /* task delay 20 ticks */
    // LOS_TaskDelay(20);

    dprintf("task2_mi try get sem g_usSemID forever .\r\n");
    /* get sem, timeout is forever */
    uwRet = LOS_SemPend(g_usSemID, LOS_WAIT_FOREVER);

    /* get sem ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task2_mi get sem ok.\r\n");
    }
    else
    {
        dprintf("task2_mi get sem fail.\r\n");
    }
    dprintf("task2_mi delete sem.\r\n");
    LOS_SemDelete(g_usSemID);
    dprintf("task2_mi exit\r\n");
    Task_finish_flag++;
    LOS_TaskDelete(g_TestTaskID02);
    
    return;
}

static VOID Example_SemTask3(VOID)
{
    UINT32 uwRet;

    dprintf("task3_hi start Sem Post.\r\n");
    /* release sem */
    LOS_SemPost(g_usSemID);
    dprintf("task3_hi Sem Post.\r\n");

    dprintf("task3_hi exit\r\n");
    Task_finish_flag++;
    LOS_TaskDelete(g_TestTaskID03);
    
    return;
}

UINT32 Example_Fifo_Semphore(VOID)
{
    UINT32 uwRet = LOS_OK;
    TSK_INIT_PARAM_S stTask1;
    TSK_INIT_PARAM_S stTask2;    
    TSK_INIT_PARAM_S stTask3;

    dprintf("--------Example_Fifo_Semphore test start!--------\r\n");
    /* create sem */
    LOS_SemCreate(0, &g_usSemID);
    dprintf("SemCreate success!\r\n");

    /* lock task schedue */
    // LOS_TaskLock();

    /* create task1 */
    memset(&stTask1, 0, sizeof(TSK_INIT_PARAM_S));
    stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_SemTask1;
    stTask1.pcName       = "MutexTsk1";
    stTask1.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask1.usTaskPrio   = TASK_PRIO_TEST;
    uwRet = LOS_TaskCreate(&g_TestTaskID01, &stTask1);
    if (uwRet != LOS_OK)
    {
        dprintf("task1 create failed .\r\n");
        return LOS_NOK;
    }
    dprintf("Create Example_SemTask1_Lo success!\r\n");

    /* create task2 */
    memset(&stTask2, 0, sizeof(TSK_INIT_PARAM_S));
    stTask2.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_SemTask2;
    stTask2.pcName       = "MutexTsk2";
    stTask2.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask2.usTaskPrio   = (TASK_PRIO_TEST - 1);
    uwRet = LOS_TaskCreate(&g_TestTaskID02, &stTask2);
    if (uwRet != LOS_OK)
    {
        dprintf("task2 create failed .\r\n");

        /* delete task 1 */
        if (LOS_OK != LOS_TaskDelete(g_TestTaskID01))
        {
            dprintf("task1 delete failed .\r\n");
        }

        return LOS_NOK;
    }
    dprintf("Create Example_SemTask2_mid success!\r\n");

    /* create stTask3 */
    memset(&stTask3, 0, sizeof(TSK_INIT_PARAM_S));
    stTask3.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_SemTask3;
    stTask3.pcName       = "MutexTsk3";
    stTask3.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask3.usTaskPrio   = (TASK_PRIO_TEST - 2);
    uwRet = LOS_TaskCreate(&g_TestTaskID03, &stTask3);
    if (uwRet != LOS_OK)
    {
        dprintf("task3 create failed .\r\n");

        /* delete task 1 */
        if (LOS_OK != LOS_TaskDelete(g_TestTaskID01))
        {
            dprintf("task1 delete failed .\r\n");
        }

        /* delete task 2 */
        if (LOS_OK != LOS_TaskDelete(g_TestTaskID02))
        {
            dprintf("task2 delete failed .\r\n");
        }

        return LOS_NOK;
    }
    
    dprintf("Create Example_SemTask3_Hi success!\r\n");

    /* unlock task schedue */
    // LOS_TaskUnlock();

    /* task delay 200 ticks */
    LOS_TaskDelay(200);
    while(Task_finish_flag<2);
    Task_finish_flag=0;
    return uwRet;
}


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
